package edu.sctu.wujiayi.month06.day20150618;

import sun.net.www.URLConnection;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by user11 on 2015/6/19.
 */
public class PostTest {
    public static String sendPost(String url,String param){
        PrintWriter out = null;
        BufferReader in = null;
        String result = "";

        try {

            URL realUrl = new URL(url);

            try {

                URLConnection con = (URLConnection) realUrl.openConnection();

                con.setRequestProperty("accept","*/*");
                con.setRequestProperty("connection","Keep-Alive");
                con.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                con.setDoInput(true);
                con.setDoOutput(true);

                out = new PrintWriter(con.getOutputStream());
                out.print(param);
                out.flush();

                in = new BufferReader(new InputStreamReader(con.getInputStream()));
                String line;
                while ((line = in.readline())!=null){
                    result += line;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }


          return result;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    return result;
    }
    public static void main(String[] args){
        String sr=PostTest.sendPost("http://localhost:6144/Home/RequestPostString","key=123&v=456");
        System.out.println(sr);
    }
}
